package com.class1group3.demo.controller;

import com.class1group3.demo.pojo.Books;
import com.class1group3.demo.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class BooksController {
    @Autowired
    private BooksService booksService;
    @GetMapping("/hello")
    public String hello() {
        return "Hello, Spring Boot! Application is running!";
    }
    @PostMapping("/select")
    public Books getBooksById(@RequestBody Books books) {
        return booksService.getBooksById(books.getId());
    }
    @PostMapping("/selectAsMap")
    public Map<String, Object> getBooksByIdAsMap(@RequestBody Books books) {
        Map<String, Object> map = booksService.getBooksByIdAsMap(books.getId());
        System.out.println(map.get("books"));
        return map;
    }
    @GetMapping("/groupByRatingDistribution")
    public List<Map<String, Object>> groupByRatingDistribution()
    {
        List<Map<String, Object>> result=booksService.groupByRatingDistribution();
        System.out.println("书籍评分分布完成");
        return result;
    }

    @GetMapping("/groupByAuthorsStats")
    public List<Map<String, Object>> groupByAuthorsStats()
    {
        List<Map<String, Object>> result=booksService.groupByAuthorsStats();
        System.out.println("作者影响力排名完成");
        return result;
    }

    @GetMapping("/selectYearlyBookCount")
    public List<Map<String, Object>> selectYearlyBookCount()
    {
        List<Map<String, Object>> result=booksService.selectYearlyBookCount();
//        for (Map<String, Object> stringObjectMap : result) {
//           for(String key:stringObjectMap.keySet()){
//               Object value=stringObjectMap.get(key);
//               System.out.println(key+":"+value);
//           }
//        }
        System.out.println("出版年份趋势分析完成");
        return result;
    }

//    @PostMapping("/yearnumber")
//    public ResponseEntity<Map<String, Object>> getYearlyStats() {
//        Map<String, Object> result = booksService.calculateYearlyStats();
//        return ResponseEntity.ok(result);
//    }   //代码没报错，但post时出现Error while processing statement: FAILED: Execution Error,
//        // return code 1 from org.apache.hadoop.hive.ql.exec.mr.MapRedTask.
//        // Permission denied: user=hive, access=EXECUTE,
//        // inode="/tmp/hadoop-yarn":root:supergroup:drwx------
@PostMapping("/selectByOrder")
public List<Map<String, Object>> getBooksByOrder(@RequestBody Books books) {
    List<Map<String, Object>> list = booksService.getBooksByOrder(books);
    System.out.println(list.get(0).get("books"));
    return list;
}
    @GetMapping("/countBooksByRating")
    public List<Map<String, Object>> countBooksByRating() {
        List<Map<String, Object>> list = booksService.countBooksByRating();
        System.out.println(list);
        return list;
    }
    @GetMapping("/countHotPublisher")
    public List<Map<String, Object>> countHotPublisher() {
        return booksService.countHotPublisher();
    }
}
